home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / lib / net / java.net / java.net.PlainSocketImpl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  3.3 KB  |  151 lines

  1. /*
  2.  * java.net.PlainSocketImpl.c
  3.  *
  4.  * Copyright (c) 1996 Systems Architecture Research Centre,
  5.  *           City University, London, UK.
  6.  *
  7.  * See the file "license.terms" for information on usage and redistribution
  8.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9.  *
  10.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <native.h>
  18. #include "../native/java.io/java.io.FileDescriptor.h"
  19. #include "java.net.SocketImpl.h"
  20. #include "java.net.InetAddress.h"
  21. #include "java.net.PlainSocketImpl.h"
  22. #include "nets.h"
  23.  
  24. /*
  25.  * Create a stream or datagram socket.
  26.  */
  27. void
  28. java_net_PlainSocketImpl_socketCreate(struct Hjava_net_PlainSocketImpl* this, long /* bool */ stream)
  29. {
  30.     int fd;
  31.     int type;
  32.  
  33.     if (stream == 0) {
  34.         type = SOCK_DGRAM;
  35.     }
  36.     else {
  37.         type = SOCK_STREAM;
  38.     }
  39.  
  40.     fd = threadedCreateSocket(AF_INET, type, 0);
  41.     if (fd < 0) {
  42.         SignalError(0, "java.io.IOException", SYS_ERROR);
  43.     }
  44.     unhand(unhand(this)->super.fd)->fd = fd;
  45. }
  46.  
  47. /*
  48.  * Connect the socket to someone.
  49.  */
  50. void
  51. java_net_PlainSocketImpl_socketConnect(struct Hjava_net_PlainSocketImpl* this, struct Hjava_net_InetAddress* daddr, long dport)
  52. {
  53.     int r;
  54.     struct sockaddr_in addr;
  55.  
  56. #if defined(BSD44)
  57.     addr.sin_len = sizeof(addr);
  58. #endif
  59.     addr.sin_family = AF_INET;
  60.     addr.sin_port = htons(dport);
  61.     addr.sin_addr.s_addr = htonl(unhand(daddr)->address);
  62.  
  63.     r = threadedConnect(unhand(unhand(this)->super.fd)->fd, (struct sockaddr*)&addr, sizeof(addr));
  64.     if (r < 0) {
  65.         SignalError(0, "java.io.IOException", SYS_ERROR);
  66.     }
  67. }
  68.  
  69. /*
  70.  * Bind this socket to an address.
  71.  */
  72. void
  73. java_net_PlainSocketImpl_socketBind(struct Hjava_net_PlainSocketImpl* this, struct Hjava_net_InetAddress* laddr, long lport)
  74. {
  75.     int r;
  76.     struct sockaddr_in addr;
  77.  
  78. #if defined(BSD44)
  79.     addr.sin_len = sizeof(addr);
  80. #endif
  81.     addr.sin_family = AF_INET;
  82.     addr.sin_port = lport;
  83.     addr.sin_addr.s_addr = unhand(laddr)->address;
  84.  
  85.     r = bind(unhand(unhand(this)->super.fd)->fd, (struct sockaddr*)&addr, sizeof(addr));
  86.     if (r < 0) {
  87.         SignalError(0, "java.io.IOException", SYS_ERROR);
  88.     }
  89. }
  90.  
  91. /*
  92.  * Turn this socket into a listener.
  93.  */
  94. void
  95. java_net_PlainSocketImpl_socketListen(struct Hjava_net_PlainSocketImpl* this, long count)
  96. {
  97.     int r;
  98.  
  99.     r = listen(unhand(unhand(this)->super.fd)->fd, count);
  100.     if (r < 0) {
  101.         SignalError(0, "java.io.IOException", SYS_ERROR);
  102.     }
  103. }
  104.  
  105. /*
  106.  * Accept a connection.
  107.  */
  108. void
  109. java_net_PlainSocketImpl_socketAccept(struct Hjava_net_PlainSocketImpl* this, struct Hjava_net_SocketImpl* sock)
  110. {
  111.     int r;
  112.     int alen;
  113.     struct sockaddr_in addr;
  114.  
  115.     alen = sizeof(addr);
  116. #if defined(BSD44)
  117.     addr.sin_len = sizeof(addr);
  118. #endif
  119.     addr.sin_family = AF_INET;
  120.     addr.sin_port = unhand(sock)->port; /* localport ?? */
  121.     addr.sin_addr.s_addr = unhand(unhand(sock)->address)->address;
  122.  
  123.     r = threadedAccept(unhand(unhand(this)->super.fd)->fd, (struct sockaddr*)&addr, &alen);
  124.     if (r < 0) {
  125.         SignalError(0, "java.io.IOException", SYS_ERROR);
  126.     }
  127. }
  128.  
  129. /*
  130.  * Return how many byts can be read without blocking.
  131.  */
  132. long
  133. java_net_PlainSocketImpl_socketAvailable(struct Hjava_net_PlainSocketImpl* this)
  134. {
  135.     return (0);
  136. }
  137.  
  138. /*
  139.  * Close this socket.
  140.  */
  141. void
  142. java_net_PlainSocketImpl_socketClose(struct Hjava_net_PlainSocketImpl* this)
  143. {
  144.     int r;
  145.  
  146.     r = close(unhand(unhand(this)->super.fd)->fd);
  147.     if (r < 0) {
  148.         SignalError(0, "java.io.IOException", SYS_ERROR);
  149.     }
  150. }
  151.